package com.guli.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.edu.client.UcenterClient;
import com.guli.edu.entity.StatisticsDaily;
import com.guli.edu.mapper.StatisticsDailyMapper;
import com.guli.edu.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author zxc
 * @since 2023-04-28
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public StatisticsDaily getStatisticsByDay(String day) {
        // 1.删除之前所统计的当前天数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated" , day);
        if(this.getOne(queryWrapper) != null){
            this.remove(queryWrapper);
        }

        // 2.调用service-ucenter模块中方法，获取当日所注册的会员总数
        Integer registerNum = ucenterClient.countRegisterMembers(day);
        // TODO 获取当日总登录数，视频观看数，新增课程数;
        Integer loginNum = 100;
        Integer videoViewNum = 200;
        Integer courseNum = 300;

        // 3.即，将当日所统计到的数据添加到表中
        StatisticsDaily dailyData = new StatisticsDaily();
        dailyData.setDateCalculated(day);
        dailyData.setRegisterNum(registerNum);
        dailyData.setLoginNum(loginNum);
        dailyData.setVideoViewNum(videoViewNum);
        dailyData.setCourseNum(courseNum);

        this.save(dailyData);

        return dailyData;
    }

    @Override
    public Map<String, Object> getChartData(String type, String begin, String end) {
        // 1.根据初始时间，结束时间和所要查询的数据类型，来获取对应数据;
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated" , begin , end); // 所查询到的记录，统计时间处于[begin , end]之间
        queryWrapper.select("date_calculated" , type); // 指定所查询到的记录中所包含字段;
        // 2.调用list方法，根据指定查询条件，查询到对应的数据集合;
        List<StatisticsDaily> dailyList = this.list(queryWrapper);

        // 3.对于所查询到的数据集合进行封装;
        List<Integer> dataList = new ArrayList<>(); // 用来存储所要查询字段的数据集合（某日的会员注册数，用户登录数，视频观看数 等等数据）
        List<String> dateList = new ArrayList<>(); // 用来存储所查询数据的日期信息；
        for (StatisticsDaily daily : dailyList) {
            // 注意 ：由于上述限制了所查询到的字段，所以当前daily对象中，有且仅仅包含dateCalculated 和 type字段;
            dateList.add(daily.getDateCalculated());
            switch (type){
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
            }
        }
        Map<String , Object> map = new HashMap<>();
        map.put("dataList" , dataList);
        map.put("dateList" , dateList);

        return map;
    }
}
